setwd("E:\\5hmc_file\\2_5hmc_yjp_bam\\ASM")
file=read.csv("./20201112做汇总表/all.FDR.sig.at.least.one.csv",head=T)
group1=c("X2B_X1T","M8_M7","M6_M5","M2_M1","M48_M47","M50_M49","M28_M27","M30_M29","M26_M25","M35_M36","M18_M17","M20_M19","M22_M21","M40_M39")
#file=file[!(file$num1==2&file$twins==1),]
#file=file[file$num1>1,]

file$pattern.not.rm.dupl.num.DC=0
for(j in 1:dim(file)[1]){
  str1=unlist(strsplit(file[j,"pattern1.DC"],":"))
  file[j,"pattern.not.rm.dupl.num.DC"]=length(str1)
}

file1=file[file$pattern.not.rm.dupl.num.DC>=1,]
file1$pattern.for.beta0.DC="bad"

for(j in 1:dim(file1)[1]){
  str1=unlist(strsplit(file1[j,"pattern1.DC"],":"))
  str1=unlist(strsplit(str1,"-"))
  str1=gsub("normal_nosig","",str1)
  str1=gsub("tumor_nosig","",str1)
  rtmp=data.frame(matrix(str1,nrow = length(str1)/2,ncol = 2,byrow = TRUE))
  x=length(unique(rtmp$X1[!rtmp$X1==""]))
  y=length(unique(rtmp$X2[!rtmp$X2==""]))
  if(x<2|y<2){
    file1[j,"pattern.for.beta0.DC"]="good"
  }
  if(x<2&y<2){
    file1[j,"pattern.for.beta0.DC"]="excellent"
  }
}
file=file1
file$pattern.nodupl.DC=NA
file$pattern.nodupl.num.DC=1
for(j in 1:dim(file)[1]){
  str1=unique(unlist(strsplit(file[j,"pattern1.DC"],":")))
  file[j,"pattern.nodupl.DC"]=paste(str1,collapse = ":")
  file[j,"pattern.nodupl.num.DC"]=length(str1)
}
file$pattern.nodupl.CC=NA
file$pattern.nodupl.num.CC=1
for(j in 1:dim(file)[1]){
  str1=unique(unlist(strsplit(file[j,"pattern1.CC"],":")))
  file[j,"pattern.nodupl.CC"]=paste(str1,collapse = ":")
  file[j,"pattern.nodupl.num.CC"]=length(str1)
}
file$pattern.nodupl.HC=NA
file$pattern.nodupl.num.HC=1
for(j in 1:dim(file)[1]){
  str1=unique(unlist(strsplit(file[j,"pattern1.HC"],":")))
  file[j,"pattern.nodupl.HC"]=paste(str1,collapse = ":")
  file[j,"pattern.nodupl.num.HC"]=length(str1)
}
file1=file
#file1=file1[file1$pattern.nodupl.num.DC==1,]
#file1=file1[file1$pattern.nodupl.num.CC==1,]


file1$pattern.nodupl.num.all=file1$pattern.nodupl.num.DC+file1$pattern.nodupl.num.CC+file1$pattern.nodupl.num.HC
#file2=file1[file1$pattern.nodupl.num.all>1,]#
file2=file1

file2$pattern.nodupl.CC.trans=""
for(j in 1:dim(file2)[1]){
  if(!stringr::str_detect(file2[j,"pattern.nodupl.CC"],pattern = "onedown_oneup")){
    str1=unique(unlist(strsplit(file2[j,"pattern.nodupl.CC"],":")))
    str1=gsub("one_is_down","tumor_down",str1)
    str1=gsub("one_is_up","tumor_up",str1)
    str1=gsub("normal_down_tumor_down","tumor_down",str1)
    str1=gsub("normal_up_tumor_up","tumor_up",str1)
    
    str1=unique(c(str1))
    file2[j,"pattern.nodupl.CC.trans"]=paste(str1,collapse = ":") 
  }else{file2[j,"pattern.nodupl.CC.trans"]="include.two.direction"}
}
file2$pattern.nodupl.HC.trans=""
for(j in 1:dim(file2)[1]){
  if(!stringr::str_detect(file2[j,"pattern.nodupl.HC"],pattern = "onedown_oneup")){
    str1=unique(unlist(strsplit(file2[j,"pattern.nodupl.HC"],":")))
    str1=gsub("one_is_down","normal_down",str1)
    str1=gsub("one_is_up","normal_up",str1)
    str1=gsub("normal_down_tumor_down","normal_down",str1)
    str1=gsub("normal_up_tumor_up","normal_up",str1)
    
    str1=unique(c(str1))
    file2[j,"pattern.nodupl.HC.trans"]=paste(str1,collapse = ":") 
  }else{file2[j,"pattern.nodupl.HC.trans"]="include.two.direction"}
}

file2$DC.pattern.valid.CC=""
file2$DC.pattern.valid.HC=""
for(j in 1:dim(file2)[1]){
  if(!file2[j,"pattern.nodupl.CC.trans"]==""){
    if(stringr::str_detect(file2[j,"pattern.nodupl.DC"],pattern = file2[j,"pattern.nodupl.CC.trans"])){
      file2[j,"DC.pattern.valid.CC"]="TRUE"
    }else{file2[j,"DC.pattern.valid.CC"]="FALSE"}
  }else{file2[j,"DC.pattern.valid.CC"]=""}
  
  if(!file2[j,"pattern.nodupl.HC.trans"]==""){
    if(stringr::str_detect(file2[j,"pattern.nodupl.DC"],pattern = file2[j,"pattern.nodupl.HC.trans"])){
      file2[j,"DC.pattern.valid.HC"]="TRUE"
    }else{file2[j,"DC.pattern.valid.HC"]="FALSE"}
  }else{file2[j,"DC.pattern.valid.HC"]=""}
}
file2$valid.num=rowSums(file2[,grep(names(file2),pattern = "DC.pattern.valid")]=="TRUE")

file2$id=paste(file2$Chr,file2$Start,sep=":")
bf=read.table("E:/5hmc_file/2_5hmc_yjp_bam/ASM/20201120/at.leat.one.AShM.DC6v6_bf.txt",header = T,sep="\t")
bf=data.frame(unitID=bf$unitID,BF_in_DC=bf$Bayes_in_DC)
file2=merge(bf,file2,by="unitID")
file=file2[,-2]
con.beta0=read.table("./20201120/DC.con.beta0.txt",head=T,sep="\t")
case.beta0=read.table("./20201120/DC.case.beta0.txt",head=T,sep="\t")

file=merge(file,bf,by="unitID")
file=merge(file,con.beta0,by="unitID")
file=merge(file,case.beta0,by="unitID")

file$beta.sig.DC="FALSE"
file[file$DC.con.FDR<0.1|file$DC.case.FDR<0.1,]$beta.sig.DC="TRUE"
write.csv(file,"./20201120/at.least.one.AShM.in.DC.add.BF.beta0.csv",quote = F,row.names = F)